<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/11/21
 * Time: 14:01
 */
namespace app\api\service;
use think\Db;
use think\Cache;
use think\Exception;
use think\log;
class IndexService
{
 private $bb='ybsc_bus_book';
 private $bc='ybsc_bus_comment';
 private $u='ybsc_user';
 private $ut='ybsc_user_tmpl';
 private $bt='ybsc_bus_tmpl';
 private $bf='ybsc_bus_form';
 public function goodsMod($data,$num,$sort){
  $list=Db::name('ybsc_goods')->where($data)->limit($num)->order($sort,'desc')->field('goods_name as name,images,price,goods_id')->select();
  if(!empty($list)){
   foreach($list as $k=>$v){
    $list[$k]['imgurl']=Db::name('ybsc_images')->where('img_id',$v['images'])->value('img_cover');
    $list[$k]['url']='/yb_shop/pages/goods/detail/index?id='.$v['goods_id'];
   }
  }
  return $list;
 }
 public function articleMod($data,$num,$sort){
  $list=Db::name('ybsc_article')->where($data)->limit($num)->order($sort,'desc')->field('title as name,short_title as description,image as imgurl,article_id,link')->select();
  if(!empty($list)){
   foreach($list as $k=>$v){
    if(empty($v['link'])){
     $list[$k]['url']='/yb_shop/pages/find_info/index?id='.$v['article_id'];
    }else{
     $list[$k]['url'] = "/yb_shop/pages/web/index?url=".$this->escape($v['link'])."&name=".$v['name'];
    }


   }
  }
  return $list;
 }
 function escape($string, $in_encoding = 'UTF-8',$out_encoding = 'UCS-2') {
  $return = '';
  if (function_exists('mb_get_info')) {
   for($x = 0; $x < mb_strlen ( $string, $in_encoding ); $x ++) {
    $str = mb_substr ( $string, $x, 1, $in_encoding );
    if (strlen ( $str ) > 1) { // 多字节字符
     $return .= '%u' . strtoupper ( bin2hex ( mb_convert_encoding ( $str, $out_encoding, $in_encoding ) ) );
    } else {
     $return .= '%' . strtoupper ( bin2hex ( $str ) );
    }
   }
  }
  return $return;
 }
 /**
  * 预订
  * @param $data
  * @return false|int|null
  */
 public function WriteBook($data)
 {
  $a=Db::name($this->bb)->where($data)->count();
  if($a>0){
   return -1;
  }
  $data['create_time']=time();
  $info=Db::name($this->bb)->insert($data);
  return $info;
 }
 /**
  * 写评论
  * @param $data
  * @return false|int|null
  */
 public function WriteComment($data)
 {
  $a=Db::name($this->bc)->where($data)->count();
  if($a>0){
   return -1;
  }
  $data['create_time']=time();
  $info=Db::name($this->bc)->update($data);
  return $info;
 }
 /**
  * 评论列表
  * @param $data
  * @return false|\PDOStatement|string|\think\Collection
  */
 public function CommentList($data,$page,$num)
 {
  if($num==0){
   $info = Db::name($this->bc)->where($data)
       ->page($page,PAGESIZE)
       ->order("create_time","desc")
       ->select();
  }else{
   $info = Db::name($this->bc)->where($data)
       ->limit($num)
       ->order("create_time","desc")
       ->select();
  }
  if(!empty($info))
  {
   foreach ($info as $key=>$value)
   {
    if($value->array_pic!=''){
     $pic = explode(",", rtrim($value->array_pic, ','));
     for($i=0;$i<count($pic);$i++){
      $pic[$i]='https://'.$_SERVER['HTTP_HOST'].'/api/'.$pic[$i];
     }
     $value->pic=$pic;
    }
    $value->create_time = date("Y-m-d",$value->create_time);
    $username=$this->getUserInfo($value->user_id);
    $value->username=$username['nick_name'];
    $value->user_headimg=$username['user_headimg'];
   }
   $rs['info']=$info;
  }else{
   $rs['info']=array();
  }
  $rs['count']= Db::name($this->bc)->where($data)->count();
  $num=(Db::name($this->bc)->where($data)->avg('fraction'))*2;
  $rs['sroce']= round($num,2);
  return json_encode($rs);
 }
 /**
  * 根据用户id获取用户信息
  * @param $uid
  * @return mixed
  */
 public function getUserInfo($uid)
 {
  $info = Db::name($this->u)->where("uid",$uid)->find();
  return $info;
 }
 /**
  * 商城首页
  * @param string $uid
  * @return array|mixed
  */
 public function mod_index($uid = "")
 {
  //判断是否存在缓存
  $rs = Cache::get($uid.'modindex');
  if($rs != false)
  {
   return $rs;
  }
  $rs = array();
  $m=Db::name($this->ut)->where('mch_id='.$uid)->find();
  if(empty($m))
  {
   return $m;
  }
  $rs = $m['index_values'];
  //写入缓存
  Cache::set($uid.'modindex',$rs,CACHE_TIME);
  return json_decode($rs);
 }
 public function get_power($data)
 {
  $m=Db::name($this->bt)->where($data)->where('is_del',1)->find();
  if(empty($m))
  {
   return $m;
  }
  $rs = json_decode($m['index_values'],true);
  $rs['name']=$m['name'];
  return $rs;
 }
 public function get_menu($uid = "")
 {
  $m=Db::name($this->ut)->where('mch_id='.$uid)->find();
  if(empty($m))
  {
   return $m;
  }
  $rs = $m['values'];
  return json_decode($rs);
 }
 public function get_tabbar($uid = "")
 {
  $m=Db::name($this->ut)->where('mch_id='.$uid)->find();
  if(empty($m))
  {
   return $m;
  }
  $rs = array();
  $index_values = $m['index_values'];
  $index_values = json_decode($index_values,true);
  if(!empty($index_values['page']))
  {
   $rs=$index_values;
  }
  return $rs;
 }
 public function get_form($data)
 {
  $data['is_del']=1;
  $m=Db::name($this->bf)->where($data)->find();
  if(empty($m))
  {
   return $m;
  }
  $rs['list'] = json_decode($m['value']);
  $rs['title']=$m['title'];
  return $rs;
 }
 function in_form($form,$name){
  $title='';
  for($i=0;$i<count($form);$i++){
   if($form[$i]['name']==$name){
    $title=$form[$i]['title'];
    break;
   }
  }
  return $title;
 }
 public function submit_form($data){
  // 启动事务
  $time=time();
  $form=json_decode($data['form'],true);
  $param=json_decode($data['param'],true);
  if(count($param)==0){
   return null;
  }
  $arr=array();
  $n=0;
  foreach($param as $k=>$v){
   $arr[$n]['name']=$k;
   $arr[$n]['value']=$v;
   $arr[$n]['title']=$this->in_form($form,$k);
   $n++;
  }
  $where=[
      'bus_form_id'=>$data['bus_form_id'],
      'user_id'=>$data['user_id'],
      'mch_id'=>$data['mch_id'],
      'param'=>json_encode($arr),
  ];
  $a=Db::name('ybsc_user_form')->where($where)->count();
  if($a>0){
   return -1;
  }
  $where['create_time']=$time;
  $form_id=Db::name('ybsc_user_form')->insertGetId($where);
  return $form_id;
 }
 public function get_ucenter($mch_id)
 {
  $m=Db::name($this->ut)->where('mch_id='.$mch_id)->find();
  if(empty($m))
  {
   return $m;
  }
  $rs = $m['center_values'];
  return $rs;
 }
}